include mips_sc/src/Makefile.testcase

.PHONY: run clean

ifndef INCLUDE_DIR
INCLUDE_DIR := ./temu/include
endif
ifndef SRC_DIR
SRC_DIR := ./temu/src
endif
ifndef BUILD_DIR
BUILD_DIR := build/
endif
DEBUG := false

# Compilation flags
CC := gcc
CFLAGS := -I$(INCLUDE_DIR) -I$(INCLUDE_DIR)/cpu -I$(INCLUDE_DIR)/memory -I$(INCLUDE_DIR)/monitor -Wall -Werror
LDFLAGS := -lreadline
CURFLAGS := -lcurses

# Files to be compiled
SRCS := $(wildcard $(SRC_DIR)/*.c) $(wildcard $(SRC_DIR)/memory/*.c) $(wildcard $(SRC_DIR)/cpu/*.c) $(wildcard $(SRC_DIR)/monitor/*.c)
TEMU_TARGET := temu

GUI_SRC := $(SRC_DIR)/OS_gui.c

ifeq ($(DEBUG), true)
CFLAGS += -g
endif

export	INCLUDE_DIR
export	SRC_DIR
export	BUILD_DIR

# ********************
# Rules of Compilation
# ********************

all: clean mips run

$(BUILD_DIR)$(TEMU_TARGET): 
	@mkdir -p $(BUILD_DIR)
	$(CC) $(SRCS) $(LDFLAGS) $(CURFLAGS) $(CFLAGS) -o $@

gdb:
	@gdb ./$(BUILD_DIR)$(TEMU_TARGET)

OS_gui: clean_gui
	$(CC) $(GUI_SRC) $(CURFLAGS) $(CFLAGS) -o $@

clean_gui:
	rm -f OS_gui
	rm -f GUI_debug.log

mips: 
	@cd mips_sc && make

clean_mips:
	@cd mips_sc && make clean

run: $(BUILD_DIR)$(TEMU_TARGET)
	@./$(BUILD_DIR)$(TEMU_TARGET) $(USER_PROGRAM)

clean:
	rm -f -r $(BUILD_DIR)
	rm -f log.txt
	rm -f -r ./mips_sc/build
	rm -f *.bin
	rm -f GUI_debug.log
	rm -f golden_trace.txt

count:
	cd temu && echo "the code lines are " && find -name *.h -or -name *.c | xargs cat | grep -v ^$$ | wc -l